package com.lg.atp.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.atp.dao.UserDao;
import com.lg.atp.entity.TblUserInfo;
import com.lg.atp.entity.TblUserRole;
import com.lg.atp.service.IUserRoleService;
import com.lg.atp.service.IUserService;


@Service("iUserService")
public class UserServiceImpl implements IUserService{

	@Autowired
	private UserDao userDao;
    @Autowired
    private IUserRoleService iUserRoleService;
	
	@Override
	public String getAllUser(TblUserInfo user,Integer pageIndex,Integer pageSize) {
		if(pageIndex != null && pageSize != null){
			PageHelper.startPage(pageIndex, pageSize);
			List<TblUserInfo> list = userDao.getAllUser(user);
			PageInfo<TblUserInfo> pageInfo = new PageInfo<TblUserInfo>(list);
			return JSONObject.toJSONString(pageInfo);
		}
		else
		return JSONObject.toJSONString(userDao.getAllUser(user));
	}


	@Transactional
	@Override
	public void updateUser(TblUserInfo user,List<TblUserRole> list) {
		userDao.updateUser(user);
		iUserRoleService.delUserRoleByUserId(user.getId());
		if(list != null && !list.isEmpty()){
			for(TblUserRole userRole:list){
				userRole.setUserId(user.getId());
			}
			iUserRoleService.insertUserRole(list);
		}
	}

	@Override
	public TblUserInfo getUserAcc(String userAccount) {
		return userDao.getUserAcc(userAccount);
	}

	@Override
	public TblUserInfo findUserById(Long id) {
		return userDao.findUserById(id);
	}

	@Override
	public TblUserInfo getUserRoleByAcc(String userAccount) {
		return userDao.getUserRoleByAcc(userAccount);
	}

	@Override
	public void updatePassword(TblUserInfo user) {
		userDao.updatePassword(user);
	}

	@Override
	public void resetPassword(TblUserInfo user) {

		userDao.resetPassword(user);
	}


	
	@Transactional
	@Override
	public Long insertUser(TblUserInfo user, List<TblUserRole> list) {
		userDao.insertUser(user);
		if(list != null && !list.isEmpty()){
			for(TblUserRole userRole:list){
				userRole.setUserId(user.getId());
			}
			iUserRoleService.insertUserRole(list);
		}
		return user.getId();
	}


	@Transactional
	@Override
	public void delUser(List<Long> userIds) {
		userDao.delUserByIds(userIds);
		iUserRoleService.delUserRoleByUserIds(userIds);
	}




}
